
package com.qingmeng.config;

import com.mybatisflex.codegen.Generator;
import com.mybatisflex.codegen.config.GlobalConfig;
import com.mybatisflex.codegen.dialect.JdbcTypeMapping;
import com.mybatisflex.codegen.generator.GeneratorFactory;
import com.zaxxer.hikari.HikariDataSource;

import java.math.BigInteger;
import java.time.LocalDateTime;
import java.util.Date;

/**
 * 使用MyBatis-Flex的代码生成功能
 * @author 清梦
 * @version 1.0.0
 * @Description 代码生成器主程序
 * @createTime 2025年08月27日 10:45:00
 */
public class GeneratorMain {


    public static void main(String[] args) {
        //配置数据源
        HikariDataSource dataSource = new HikariDataSource();
        dataSource.setJdbcUrl(GeneratorConfig.JDBC_URL);
        dataSource.setUsername(GeneratorConfig.USERNAME);
        dataSource.setPassword(GeneratorConfig.PASSWORD);

        //创建配置内容
        GlobalConfig globalConfig = createGlobalConfig();
        //通过 datasource 和 globalConfig 创建代码生成器
        Generator generator = new Generator(dataSource, globalConfig);
        // 生成自定义模板
        GeneratorFactory.registerGenerator("dao",new CustomDaoGenerator());
        GeneratorFactory.registerGenerator("vo",new CustomVOGenerator());
        GeneratorFactory.registerGenerator("saveOrUpdateDTO",new CustomSaveOrUpdateDTOGenerator());
        GeneratorFactory.registerGenerator("pageDTO",new CustomPageDTOGenerator());
        //生成代码
        generator.generate();
    }

    public static GlobalConfig createGlobalConfig() {
        //创建配置内容
        GlobalConfig globalConfig = new GlobalConfig();

        // 全局配置
        globalDetailConfig(globalConfig);
        //设置生成 entity
        entityConfig(globalConfig);
        //设置生成 mapper
        mapperConfig(globalConfig);
        //设置生成 service
        serviceConfig(globalConfig);
        //设置生成 controller
        controllerConfig(globalConfig);
        // 设置def配置
        defConfig(globalConfig);


        return globalConfig;
    }


    /**
     * 全局详细信息配置
     *
     * @param globalConfig 全局配置
     * @author qingmeng
     */
    private static void globalDetailConfig(GlobalConfig globalConfig) {
        globalConfig.setBasePackage(GeneratorConfig.BASE_PACKAGE);
        globalConfig.setLogicDeleteColumn(GeneratorConfig.LOGIC_DELETE_FILED_NAME);
        globalConfig.setGenerateTable(GeneratorConfig.TABLE_LIST.toArray(new String[0]));
        globalConfig.getJavadocConfig().setAuthor(GeneratorConfig.AUTHOR);
        //设置项目的JDK版本，项目的JDK为14及以上时建议设置该项，小于14则可以不设置
        globalConfig.setEntityJdkVersion(17);

    }


    /**
     * 实体配置
     *
     * @param globalConfig 全局配置
     * @author qingmeng
     */
    private static void entityConfig(GlobalConfig globalConfig) {
        globalConfig.getTemplateConfig().setEntity("/templates/entity.tpl");
        globalConfig.setEntityGenerateEnable(true);
        globalConfig.getEntityConfig().setOverwriteEnable(true);
        JdbcTypeMapping.registerMapping(LocalDateTime.class, Date.class);
        JdbcTypeMapping.registerMapping(BigInteger.class, Long.class);
        JdbcTypeMapping.registerMapping(Boolean.class, Integer.class);
    }

    /**
     * def配置
     *
     * @param globalConfig 全局配置
     * @author qingmeng
     */
    private static void defConfig(GlobalConfig globalConfig) {
        globalConfig.setTableDefGenerateEnable(true);
        globalConfig.getTableDefConfig().setOverwriteEnable(true);
    }

    /**
     * 映射器配置
     *
     * @param globalConfig 全局配置
     * @author qingmeng
     */
    private static void mapperConfig(GlobalConfig globalConfig) {
        //设置生成 entity 并启用 Lombok
        globalConfig.getTemplateConfig().setMapper("/templates/mapper.tpl");
        globalConfig.getMapperConfig().setOverwriteEnable(true);
        globalConfig.setMapperGenerateEnable(true);
    }

    /**
     * DAO 配置
     *
     * @param globalConfig 全局配置
     * @author qingmeng
     */
    private static void serviceConfig(GlobalConfig globalConfig) {
        globalConfig.getTemplateConfig().setService("/templates/service.tpl");
        globalConfig.getTemplateConfig().setServiceImpl("/templates/serviceImpl.tpl");
        globalConfig.getServiceConfig().setOverwriteEnable(true);
        globalConfig.getServiceImplConfig().setOverwriteEnable(true);
        globalConfig.setServiceGenerateEnable(true);
        globalConfig.setServiceImplGenerateEnable(true);
    }

    /**
     * 控制器配置
     *
     * @param globalConfig 全局配置
     * @author qingmeng
     */
    private static void controllerConfig(GlobalConfig globalConfig) {
        globalConfig.getTemplateConfig().setController("/templates/controller.tpl");
        globalConfig.getControllerConfig().setOverwriteEnable(true);
        globalConfig.setControllerGenerateEnable(true);

    }
}